# vim: ts=4:sw=4:syntax=perl

use gri_secmgr qw(get_user_name got_cookie);
use gri_conversions;
use gri_mime;

sub mm_send_email {
my ($portal,$repname,$repnum,$email,$convert)=@_;

	#########################################################################
	# Work out whether MIME types are supported by the Perl install.	#
	#########################################################################
	my $mine_info;
	my $mime_file=$::GRI_FRONTEND->get_variable("MIME_FILE");
	if(defined($mime_file)) {
		$mime_info=new gri_mime($mime_file);
	}
	my $mime_available=1;
	eval "use MIME::Lite;";
	my $p2=hex_to_string($portal);
	my $r2=hex_to_string($repname);
	my $rn2=hex_to_string($repnum);
	my $actual_filename=undef;
	if($@) {
		$mime_available=0;
	} else {
		my ($x,$y)=$::GRI_FRONTEND->get_report_filename($p2,$r2,$rn2);
		if($x) {
			$actual_filename=$y;
		}
	}

	if(! got_cookie(name => "gri_dbm_intranet")) {
		print STDERR "mm_send_email: User not logged in - no email sent!\n";
		return "Unable to send mail - user not logged in.";
	}
	my $v=get_user_name();
	if($v eq "public") {
		print STDERR "mm_send_email: User logged in as public - no email sent!\n";
		return "Unable to send mail - user logged in as 'public'";
	}
	$email=$::GRI_FRONTEND->get_user_email_address($v);
	if(defined($email) && length($email)) {
		my $x;
		chomp $email;
		load_macro("mm_portal_file_view");
		my $title=$::GRI_FRONTEND->get_report_name($r2);
		my $type=$::GRI_FRONTEND->get_report_type($r2);
		my $fd;
		open($fd,"|/usr/sbin/sendmail -i -t");
		my @I=$mime_info->get_type_info($type);
		if(defined($I[1])) {
			$type=$I[1];
		}
		if($mime_available) {
			my $ext=$type; $ext =~ s/.*\///;
			my $afilename;
			if(defined($actual_filename)) {
				$afilename=$actual_filename;
			} else {
				$afilename="data.$ext";
			}
			# Handle dynamic conversion of information ... #
			if($convert eq "1") {
				my @rinfo=$::GRI_FRONTEND->get_portal_report_details($p2,$r2);
				my $tmpdir=$::GRI_FRONTEND->get_tmp_content_dir();
				my $gri_conv=new gri_conversions(tmpdir=>$tmpdir);
				my $is_convertable=$gri_conv->can_convert($rinfo[4],$rinfo[6]);
				if($is_convertable==0) {
					return "Unable to convert type '$rinfo[4]' to '$rinfo[6]'.";
				}
				$x=mm_portal_file_view($portal,$repname,$repnum,"?repmtime - not defined?",0,2);
				my ($rc,$info)=$gri_conv->do_convert($rinfo[4],$rinfo[6],[$x],$rinfo[7]);
				if($rc==1) {
					$x=$info; $type=$rinfo[6];
					my @I=$mime_info->get_type_info($type);
					if(defined($I[1])) {
						$type=$I[1];
					}
					my $a1=$rinfo[4]; my $a2=$rinfo[6];
					$afilename =~ s/\.$a1/\.$a2/g;
				} else {
					return "Dynamic conversion failure: $info";
				}
			} else {
				if($type eq "text/plain") {
					$x=mm_portal_file_view($portal,$repname,$repnum,"?repmtime - not defined?",0,1);
				} else {
					$x=mm_portal_file_view($portal,$repname,$repnum,"?repmtime - not defined?",0,2);
				}
			}
			my $msg = MIME::Lite->new(To	  => $email,
						  From    => "DBM Reporting Portal",
						  Subject => $title,
						  Type	  => $type,
						  Filename=> $afilename,
						  Encoding=> 'base64',
						  Data    => $x,
						  Disposition => 'attachment');
			$msg->print($fd);
		} else {
			if($convert eq "1") {
				return "Conversions only available via MIME support.";
			}
			$x=mm_portal_file_view($portal,$repname,$repnum,"?repmtime - not defined?",0,1);
			print $fd "Mime-Version: 1.0\nContent-type: text/html; charset=\"iso-8859-1\"\n";
			print $fd "From: DBM Reporting Portal\n";
			print $fd "To: $email\n";
			print $fd "Subject: $title\n\n";
			print $fd $x;
		}
		close($fd);
		
		if($x =~ /(<frame\s+.*?src=)\"(.+?)\"/i) {
			return "The email has been successfully send to '$email'.\n<br><font color=red>Please note the content contained frames - it might not present correctly in an email.</font>";
		}
		return "The email has been successfully send to '$email'.";
	}
	return "Failed to send message - unable to resolve name '$v' to email address.";
}

1;
